<template>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm">
        <el-form-item label="请选择角色：" prop="role">
            <el-select v-model="ruleForm.role" placeholder="请选择角色：">

                <el-option v-for="(v, i) in offClickFun" :key="i" :label="v.roleName" :value="v.roleId"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="用户名：" prop="username">
            <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码：" prop="password">
            <el-input type="text" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
        </el-form-item>
    </el-form>
</template>
<script>
import { addUserList } from "@/api/SettingsApi/index.js"

export default {
    props: ['offClickFun'],
    data() {

        var validaterole = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请选择角色'));
            } else {

                callback();
            }
        };
        var validateuser = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请选择用户名'));
            } else {

                callback();
            }
        }; var validatepass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请选择密码'));
            } else {

                callback();
            }
        };
        return {
            ruleForm: {
                role: '',
                username: "",
                password: ""

            },
            rules: {
                role: [
                    { validator: validaterole, trigger: 'blur' }
                ], username: [
                    { validator: validateuser, trigger: 'blur' }
                ], password: [
                    { validator: validatepass, trigger: 'blur' }
                ],

            }
        };
    },
    computed: {
        isAdmin() {
            let role = this.$store.getters.role;
            console.log(role);
            return role.indexOf('admin') >= 0
        }
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    console.log(this.ruleForm);
                    let data = {
                        roleIds: this.ruleForm.role,
                        nickName: this.ruleForm.username,
                        userName: this.ruleForm.username,
                        password: this.ruleForm.password,
                    }
                    addUserList(data).then(res => {
                        if (res.code == 200) {
                            this.ruleForm.role = "";
                            this.ruleForm.username = "";
                            this.ruleForm.username = "";
                            this.ruleForm.password = "";
                            this.$emit("UserListFun", res)
                        } 
                    })


                } else {
                    console.log('error submit!!');
                    return false;
                }

            });
        },

    }
}
</script>
<style lang="less" scoped>
.el-select-dropdown {
    width: 267px !important;
    height: 89px !important;
    background: #011A41 !important;
    box-shadow: 3px 4px 9px 4px #011A41 !important;
    opacity: 0.92 !important;
    border: 0px solid #2EABD4 !important;
}

.el-form-item {

    display: flex;
    margin-bottom: 20px;
    justify-content: center;
}

.el-form /deep/ .el-form-item__label {
    width: 100px;
    color: aliceblue;
}

.el-form /deep/ .el-input__inner {
    width: 200px !important;
    height: 27px !important;
    background-color: #031231 !important;
    opacity: 1 !important;
    border: 0px solid #5B9FC3 !important;
}

.el-form-item {
    margin-bottom: 15px !important;

    .el-form-item__content {
        .el-input {
            /deep/ input {
                border-radius: 0px;
            }

            /deep/ .el-input__inner {
                border-radius: 0px;

            }
        }

    }

}



.el-button--primary {
    width: 140px;
    height: 40px;
    border: 1px solid #113D71;
    background: none;
    color: white;
    border-radius: 0;
    margin-left: 80px;
    margin-top: 5px;
    background: #00194bFF;
}
</style>